package com.elmcode.server.handle;

import com.elmcode.server.exception.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(ParamsException.class)
    public Map<String,String> handleParamException(ParamsException paramsException){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",paramsException.getMessage());
        log.error(paramsException.getMessage());
        return info;
    }

    @ExceptionHandler(Exception.class)
    public Map<String,String> handleParamException(Exception e){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",e.getMessage());
        log.error(e.getMessage());
        return info;
    }

    //BusException
    @ExceptionHandler(BusException.class)
    public Map<String,String> handleBusException(BusException e){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",e.getMessage());
        log.error(e.getMessage());
        return info;
    }


    //CategoryExistException
    @ExceptionHandler(CategoryException.class)
    public Map<String,String> CategoryExistException(CategoryException e){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",e.getMessage());
        log.error(e.getMessage());
        return info;
    }

    //FlavorExistException
    @ExceptionHandler(FlavorException.class)
    public Map<String,String> FlavorExistException(FlavorException e){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",e.getMessage());
        log.error(e.getMessage());
        return info;
    }


    @ExceptionHandler(TokenException.class)
    public Map<String,String> TokenNotMatchException(TokenException e){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",e.getMessage());
        log.error(e.getMessage());
        return info;
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Map<String,String> handlerNotFoundException(NoHandlerFoundException handlerFoundException){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg","请求路径错误");
        log.error(handlerFoundException.getMessage());
        return info;
    }

    @ExceptionHandler(UserException.class)
    public Map<String,String> UserExistException(UserException userExistException){
        HashMap<String, String> info = new HashMap<>();
        info.put("msg",userExistException.getMessage());
        log.error(userExistException.getMessage());
        return info;
    }

}
